package com.cy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cy.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author tang
 * @data 2021/9/17
 * @apiNode
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    /**
     * 构建RestTemplate对象,并将此对象交给spring管理
     * 后续我们会通过此对象进行远程服务调用
     * @return
     */
    @Bean
    public RestTemplate restTemplate(){
        System.out.println("==restTemplate==");
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    public RestTemplate loadBalancerRestTemplate(){
        return new RestTemplate();
    }
        @Autowired
        private LoadBalancerClient loadBalancerClient;

        @Autowired
        private RestTemplate restTemplate;

        @Value("${spring.application.name}")
        private String appName;

        @Autowired
        private RestTemplate loadBalancerRestTemplate;

        @Autowired
        private ConsumerService consumerService;

        //构建一个线程安全并且可实现自增自减操作的整数对象
        private AtomicLong aLong = new AtomicLong(1);

    @GetMapping("/con")
    public String doRestEcho1() throws InterruptedException {
        //降级熔断测试
        long l = aLong.incrementAndGet();
        if (l % 2 == 0){
            //系统设计时会认为响应设计超过200毫秒为慢调用
            Thread.sleep(200);//模拟耗时操作
        }
        //调用服务提供方API(http://ip:port/path)
        //1.定义要调用的API
        String url = "http://localhost:8081/provider/echo/"+appName;
        //2.谁去访问这个API
        return restTemplate.getForObject(url,String.class);
    }

    //负载均衡
    @GetMapping("/con2")
    public String doRestEcho2(){
        consumerService.doGetResource();
        //1.获取服务实例
        ServiceInstance instance = loadBalancerClient.choose("sca-provider");
        //2.基于restTemplate进行服务实例对象
        String ip = instance.getHost();  //获取ip
        int port = instance.getPort();  //获取port
        String url = "http://"+ip+":"+port+"/provider/echo/{appName}";

        return restTemplate.getForObject(url, String.class, appName);
    }
    @GetMapping("/con3")
    public String doEcho3(){
        consumerService.doGetResource();
        String url = String.format("http://sca-provider/provider/echo/%s", appName);
        return loadBalancerRestTemplate.getForObject(url, String.class);
    }

    @GetMapping("/findById")
    @SentinelResource("res")
    public String doFindById(Integer id){
        return "find by " + id;
    }
}
